package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.InspectionMapper;
import com.yl.applicationsecurity.mapper.KunpengSqlInjectionMapper;
import com.yl.applicationsecurity.pojo.Inspection;
import com.yl.applicationsecurity.pojo.KunpengSqlInjection;
import com.yl.applicationsecurity.pojo.ScriptState;
import com.yl.applicationsecurity.service.InspectionService;
import com.yl.applicationsecurity.service.KunpengSqlInjectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-03-25 17:23:57
 */
@Service
public class KunpengSqlInjectionServiceImpl extends ServiceImpl<KunpengSqlInjectionMapper, KunpengSqlInjection> implements KunpengSqlInjectionService {

    @Override
    public PageInfo<KunpengSqlInjection> findListByKunpengSqlInjection(Integer[] statusArray,String[] nationSelect, KunpengSqlInjection kunpengSqlInjection, Date startCreateDate, Date endCreateDate, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<KunpengSqlInjection> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(kunpengSqlInjection.getUrl())){
            lambdaQueryWrapper.like(KunpengSqlInjection::getUrl,kunpengSqlInjection.getUrl());
        }

        if ( StringUtils.isNotEmpty(kunpengSqlInjection.getPath())){
            lambdaQueryWrapper.like(KunpengSqlInjection::getPath,kunpengSqlInjection.getPath());
        }


        if ( StringUtils.isNotEmpty(kunpengSqlInjection.getParameter())){
            lambdaQueryWrapper.like(KunpengSqlInjection::getParameter,kunpengSqlInjection.getParameter());
        }

        if (statusArray!=null&&statusArray.length!=0){
            lambdaQueryWrapper.in(KunpengSqlInjection::getStatus,statusArray);
        }

        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(KunpengSqlInjection::getNation,nationSelect);
        }

        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(KunpengSqlInjection::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(KunpengSqlInjection::getCreateDate, endCreateDate);
        }

        lambdaQueryWrapper.orderByDesc(KunpengSqlInjection::getCreateDate);//根据首次发现时间排序
        List<KunpengSqlInjection> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<KunpengSqlInjection> pageInfo = new PageInfo<KunpengSqlInjection>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;


    }

    @Override
    public int updateStatus(String[] ids, Integer updateStatus) {
        LambdaUpdateWrapper<KunpengSqlInjection> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(KunpengSqlInjection::getStatus,updateStatus).in(KunpengSqlInjection::getId , ids);

        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int insert(KunpengSqlInjection kunpengSqlInjection) {

        kunpengSqlInjection.setId(UUID.randomUUID().toString());
        kunpengSqlInjection.setStatus(0);//状态修改为待处理
        kunpengSqlInjection.setCreateDate(new Date());//创建时间
        if(kunpengSqlInjection.getUrl()!=null&kunpengSqlInjection.getNation()!=null&kunpengSqlInjection.getPath()!=null&kunpengSqlInjection.getParameter()!=null){
            return baseMapper.insert(kunpengSqlInjection);
        }


        return 0;
    }
}

